package de.ubschmidt.capi;

import java.nio.ByteBuffer;

/**
 * This message initiates the setting up of a physical connection.
 *
 * <p> An application only needs to provide the relevant parameters
 * (i.e. Controller, CIP value and usually called party number). Every
 * other structure can be empty (length zero). In this case, the default
 * values as described in Chapter 6 are used.
 *
 * <p>Note
 * <p>If an application provides BC, LLC and/or HLC, the parameters are
 * used without checking the resulting combination.
 *
 * <p>The absence (i.e. coding as an empty structure) of the B protocol
 * parameter results in the default protocol behavior: ISO 7776 (X.75) and
 * window size seven. This is a recommended selection to obtain general
 * connectivity with the benefits of HDLC error recovery. Note that ISO 7776
 * defines a default maximum data length of 128 octets, whereas
 * COMMON-ISDN-API is able to handle up to at least 2048 octets, depending
 * on the CAPI_REGISTER parameters values of the given application.
 */
public class ConnectReq
        extends CapiActiveMessage
        implements CalledPartyNumberAccessor, ControllerProperty {

    /** The controller number. */
    private int _controller = 1;

    /** The CIP. */
    private Cip _cipValue = Cip.ANY;

    /** The called party number. */
    private CalledPartyNumber _calledPartyNumber = new CalledPartyNumber();

    /** The calling party number. */
    private CallingPartyNumber _callingPartyNumber = new CallingPartyNumber();

    /** The called party subaddress. */
    private SubAddress _calledPartySubAddress = new SubAddress();

    /** The calling party subaddress. */
    private SubAddress _callingPartySubAddress = new SubAddress();

    /** Not yet implemented. */
    private Object _bProtocol;
    private Object _bc;
    private Object _llc;
    private Object _hlc;
    private Object _additionalInfo;

    /**
     * Creates a new CONNECT_REQ CAPI message.
     *
     * <p>The following parameters must be set:
     * <ul>
     * <li>CIP value
     * <li>called party number
     * </ul>
     *
     * All other parameters may be omitted or have reasonable defaults.
     */
    public ConnectReq() {
        super(Command.CONNECT_REQ);
    }

    /** {@inheritDoc} */
    public int getController() {
        return _controller;
    }

    /** {@inheritDoc} */
    public void setController(final int controller) {
        _controller = controller;
    }

    /**
     *
     * @param cipValue
     */
    public void setCipValue(final Cip cipValue) {
        _cipValue = cipValue;
    }

    /**
     *
     * @return
     */
    public Cip getCipValue() {
        return _cipValue;
    }

    /** {@inheritDoc} */
    public CalledPartyNumber getCalledPartyNumber() {
        return _calledPartyNumber;
    }

    /** {@inheritDoc} */
    protected void serializeTo(final ByteBuffer byteBuffer) {
        byteBuffer.putInt(_controller);

        _cipValue.writeTo(byteBuffer);
        _calledPartyNumber.writeTo(byteBuffer);
        _callingPartyNumber.writeTo(byteBuffer);
        _calledPartySubAddress.writeTo(byteBuffer);
        _callingPartySubAddress.writeTo(byteBuffer);

        // not yet implemented parameters.
        byteBuffer.put((byte) 0);
        byteBuffer.put((byte) 0);
        byteBuffer.put((byte) 0);
        byteBuffer.put((byte) 0);
        byteBuffer.put((byte) 0);
    }

    /** {@inheritDoc} */
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();

        sb.append(getClass().getSimpleName());
        sb.append("[").append(super.toString());
        sb.append(" ctrl=").append(getController());
        sb.append(" cip=").append(getCipValue());
        sb.append(" called#=").append(_calledPartyNumber);
        sb.append(" calling#=").append(_callingPartyNumber);
        sb.append(" calledSub#=").append(_calledPartySubAddress);
        sb.append(" callingSub#=").append(_callingPartySubAddress);
        sb.append(" ...");
        sb.append("]");

        return sb.toString();
    }

}
